#import "UserData.h"
#import <AFNetworking/AFNetworking.h>
static UserData *userdata = nil;

@implementation UserData

+ (UserData*)shareUser{
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        userdata = [[UserData alloc]init];
    });
    return userdata;
}

+(void)LoadUser:(NSDictionary *)dic{
    UserData * user = [UserData shareUser];
    user.Name = dic[@"Name"];
    user.Email = dic[@"Email"];
    user.UserId = dic[@"ID"];
    user.Class = [dic[@"Class"] intValue];
    user.bio = dic[@"Info"][@"Bio"];
    user.Gender = [dic[@"Info"][@"Gender"] intValue];
    user.LikeCount = [dic[@"LikeCount"] intValue];
    user.ContentCount = [dic[@"ContenCount"] intValue];
    user.FollowerCount = [dic[@"FollowerCount"] intValue];
    user.FollowingCount = [dic[@"FollowingCount"] intValue];
}

+ (void) UpdateUser{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    NSString *selfURL = @"http://172.18.178.56/api/user/info/self";
    [manager GET:selfURL parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable response) {
        NSLog(@"\nUser Data: %@", response);
        NSDictionary *userData = (NSDictionary *)response;
        [UserData LoadUser:userData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"Update failed");
    }];
}


@end
